package com.shop.service.impl;

import com.shop.constant.Constants;
import com.shop.dao.GoodsDao;
import com.shop.dao.impl.GoodsDaoImpl;
import com.shop.entity.*;
import com.shop.exception.*;
import com.shop.factory.ObjectFactory;
import com.shop.service.GoodsService;
import com.shop.transaction.TransactionManager;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class GoodsServiceImpl implements GoodsService {
    private GoodsDao dao = (GoodsDaoImpl) ObjectFactory.getInstance(Constants.GOODS_DAO_KEY);

    /**
     * 添加商品
     * @param goodsDetailImgVO
     * @throws DAOException
     * @throws GoodsIsNotExistException
     * @throws GoodDetailIsExistException
     * @throws ImgPathIsExistException
     */
    @Override
    public void addGood(GoodsDetailImgVO goodsDetailImgVO) throws DAOException, GoodsIsNotExistException, GoodDetailIsExistException, ImgPathIsExistException {
        TransactionManager tm = new TransactionManager();
        try {
            tm.openTransaction();
            int gft_id = dao.queryFirstByName(goodsDetailImgVO.getGft_name()).getGft_id();
            int gst_id = dao.querySecondByName(goodsDetailImgVO.getGst_name()).getGst_id();
            int gtt_id = dao.queryThirdByName(goodsDetailImgVO.getGtt_name()).getGtt_id();
            GoodsFourType goodsFourType = new GoodsFourType(gtt_id,goodsDetailImgVO.getGfour_name());

            GoodsFourType goodsFourType1 = dao.queryGfourByIdName(gtt_id,goodsDetailImgVO.getGfour_name());
            if(goodsFourType1==null){
                dao.addGoodsFourType(goodsFourType);
            }
            GoodsFourType goodsFourType2 = dao.queryGfourByIdName(gtt_id,goodsDetailImgVO.getGfour_name());
            Goods goods = dao.queryGoodsByName(goodsDetailImgVO.getG_name());
            if(goods==null){
                    throw new GoodsIsNotExistException("商品不存在!");
            }
//            Goods goods = new Goods(goodsDetailImgVO.getG_name(),goodsDetailImgVO.getG_data(),goodsDetailImgVO.getG_datail());
//            dao.addGoods(goods);
//            Goods goods1 = dao.queryGoodsByName(goodsDetailImgVO.getG_name());
//            GoodDetail destGoodDetail = dao.queryGoodDetailByGId(goods.getG_id());
//            if(destGoodDetail!=null){
//                throw new GoodDetailIsExistException("商品详情已存在!");
//            }
            GoodDetail goodDetail = new GoodDetail(gft_id,gst_id,gtt_id,goodsFourType2.getGfour_id(),goodsDetailImgVO.getGd_price(),goodsDetailImgVO.getGd_num(),goods.getG_id(),goodsDetailImgVO.getS_id(),goodsDetailImgVO.getUu_id());
            dao.addGoodDetail(goodDetail);
            GoodDetail goodDetail1 = dao.queryGoodDetailByUuid(goodsDetailImgVO.getUu_id());
            ImgPath destImgPath = dao.queryImgPathById(goodDetail1.getGd_id());
            if(destImgPath!=null){
                throw new ImgPathIsExistException("商品图片已存在!");
            }
            ImgPath imgPath = new ImgPath(goodsDetailImgVO.getImg_path(),goodDetail1.getGd_id());
            dao.addImgPath(imgPath);

            tm.commit();
        } catch (DAOException e) {
            tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
//         finally {
//            TransactionManager.close();
//        }
    }

    /**
     * 删除商品
     * @param gd_id
     * @throws DAOException
     * @throws ImgIsNotExistException
     * @throws GoodDetailIsNotExistException
     * @throws GoodsIsNotExistException
     */
    @Override
    public void deleteGood(int gd_id) throws DAOException, ImgIsNotExistException, GoodDetailIsNotExistException, GoodsIsNotExistException {
        TransactionManager tm = new TransactionManager();
        try {
            tm.openTransaction();
            ImgPath imgPath = dao.queryImgPath(gd_id);
            if(imgPath==null){
                throw new ImgIsNotExistException("商品图片不存在!");
            }
            GoodDetail goodDetail = dao.queryGoodDetailById(gd_id);
            if(goodDetail==null){
                throw new GoodDetailIsNotExistException("商品详情不存在!");
            }
            int g_id = goodDetail.getG_id();
            Goods goods = dao.queryGoodsById(g_id);
            if(goods==null){
                throw new GoodsIsNotExistException("商品不存在!");
            }
            int gfour_id = goodDetail.getGfour_id();
            dao.deleteImgPath(imgPath.getImg_id());
            dao.deleteGoodDetail(gd_id);
            GoodDetail goodDetail1 = dao.queryGoodDetailByGfourId(gfour_id);
            if(goodDetail1==null) {
                dao.deleteGoodsFourType(gfour_id);
            }
            //dao.deleteGoods(g_id);
            tm.commit();
        } catch (DAOException e) {
            tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
//        finally {
//            TransactionManager.close();
//        }
    }

    /**
     * 修改商品
     * @param updateBindData
     * @throws DAOException
     * @throws GoodsIsNotExistException
     * @throws GoodDetailIsNotExistException
     * @throws ImgIsNotExistException
     */
    @Override
    public void updateGood(UpdateBindData updateBindData) throws DAOException,GoodsIsNotExistException, GoodDetailIsNotExistException, ImgIsNotExistException {
        TransactionManager tm = new TransactionManager();
        try {
            tm.openTransaction();
//            GoodsFourType goodsFourType = new GoodsFourType(updateBindData.getGfour_id(),updateBindData.getGtt_id(),updateBindData.getGfour_name());
//            dao.updateGoodsFourType(goodsFourType);
            GoodsFourType  goodsFourType = dao.queryGfourByIdName(updateBindData.getGtt_id(),updateBindData.getGfour_name());
            if(goodsFourType==null){
                GoodsFourType goodsFourType1 = new GoodsFourType(updateBindData.getGtt_id(),updateBindData.getGfour_name());
                dao.addGoodsFourType(goodsFourType1);
            }
            GoodsFourType goodsFourType2 = dao.queryGfourByIdName(updateBindData.getGtt_id(),updateBindData.getGfour_name());
//            Goods goods1 = dao.queryGoodsById(updateBindData.getG_id());
//            if(goods1==null){
//                throw new GoodsIsNotExistException("商品不存在!");
//            }
//            Goods goods = new Goods(updateBindData.getG_id(),updateBindData.getG_name(),updateBindData.getG_data(),updateBindData.getG_datail());
//            dao.updateGoods(goods);
            GoodDetail goodDetail1 = dao.queryGoodDetailByUuid(updateBindData.getUu_id());
            if(goodDetail1==null){
                throw new GoodDetailIsNotExistException("商品详情不存在!");
            }
            GoodDetail goodDetail = new GoodDetail(goodDetail1.getGd_id(),updateBindData.getGft_id(), updateBindData.getGst_id(),updateBindData.getGtt_id(), goodsFourType2.getGfour_id(),updateBindData.getGd_price(),updateBindData.getGd_num(),goodDetail1.getG_id(),goodDetail1.getS_id(),updateBindData.getUu_id());
            dao.updateGoodDetail(goodDetail);

            ImgPath imgPath1 = dao.queryImgPathById(updateBindData.getImg_id());
            if(imgPath1==null){
                throw new ImgIsNotExistException("商品图片不存在!");
            }
            ImgPath imgPath = new ImgPath(updateBindData.getImg_id(),updateBindData.getImg_path(),imgPath1.getGd_id());
            dao.updateImgPath(imgPath);

            tm.commit();
        } catch (DAOException e) {
            tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
//        finally {
//            TransactionManager.close();
//        }
    }

    /**
     * 返回数据
     * @param gft_name
     * @param gst_name
     * @return
     * @throws DAOException
     */
    @Override
    public Map<String, Object> getBack(String gft_name,String gst_name) throws DAOException {

        Map<String,Object> map = new LinkedHashMap<>();
        if(gft_name == null || "".equals(gft_name)) {
            List<GoodsFirstType> gft = dao.queryAllGft();
            map.put("first",gft);
            List<Goods> goods = dao.queryAllGoods();
            map.put("goods",goods);
        }else if(gst_name == null || "".equals(gst_name)) {
            List<GoodsFirstType> gft = dao.queryAllGft();
            map.put("first", gft);
            int gft_id = dao.queryFirstByName(gft_name).getGft_id();
            List<GoodsSecondType> gst = dao.queryAllGst(gft_id);
            map.put("second", gst);
            List<Goods> goods = dao.queryAllGoods();
            map.put("goods",goods);
        }else {
            List<GoodsFirstType> gft = dao.queryAllGft();
            map.put("first", gft);
            int gft_id = dao.queryFirstByName(gft_name).getGft_id();
            List<GoodsSecondType> gst = dao.queryAllGst(gft_id);
            map.put("second", gst);
            int gst_id = dao.querySecondByName(gst_name).getGst_id();
            List<GoodsThirdType> gtt = dao.queryAllGtt(gst_id);
            map.put("third",gtt);
            List<Goods> goods = dao.queryAllGoods();
            map.put("goods",goods);
            }
        return map;
    }

    /**
     * 查询所有商品
     * @param s_id
     * @return
     * @throws DAOException
     * @throws GoodListIsNotExistException
     */
    @Override
    public List<GoodsListVO> queryAllGoods(int s_id) throws DAOException, GoodListIsNotExistException {
         List<GoodsListVO> list = dao.queryAllGoodDetail(s_id);
         if(list==null){
             throw new GoodListIsNotExistException("商家不存在商品");
         }
        return list;
    }

    /**
     * 模糊查询商品
     * @param s_id
     * @param g_name
     * @return
     * @throws DAOException
     * @throws GoodListIsNotExistException
     */
    @Override
    public List<GoodsListVO> queryAllGoodsLike(int s_id,String g_name) throws DAOException, GoodListIsNotExistException {
        List<GoodsListVO> list = dao.queryAllGoodDetailLike(s_id,g_name);
        if(list==null){
            throw new GoodListIsNotExistException("商家不存在这类商品");
        }
        return list;
    }

    /**
     * 返回商品
     * @param gd_id
     * @return
     * @throws DAOException
     * @throws GoodsIsNotExistException
     */
    @Override
    public UpdateBindData getBackGoods(int gd_id) throws DAOException, GoodsIsNotExistException {
        UpdateBindData updateBindData= dao.getbackGoods(gd_id);
        if(updateBindData == null){
            throw new GoodsIsNotExistException("商品不存在");
        }

        return updateBindData;
    }

    /**
     * 下架商品
     * @param g_id
     * @param s_id
     * @throws DAOException
     */
    @Override
    public void soldGoods(int g_id, int s_id) throws DAOException {
        TransactionManager tm = new TransactionManager();
        try {
            tm.openTransaction();
            List<GoodDetail> goodDetail = dao.queryGoodDetailByGidSid(g_id,s_id);
            for (int i=0; i<goodDetail.size();i++) {
                dao.deleteImgPathByGdid(goodDetail.get(i).getGd_id());
                dao.deleteGoodDetail(goodDetail.get(i).getGd_id());
                GoodDetail goodDetail1 = dao.queryGoodDetailByGfourId(goodDetail.get(i).getGfour_id());
                if(goodDetail1==null) {
                    dao.deleteGoodsFourType(goodDetail.get(i).getGfour_id());
                }
            }
            dao.deleteGoods(g_id);
            tm.commit();
        } catch (DAOException e) {
            tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
    }

    /**
     * 上架商品
     * @param goods
     * @throws DAOException
     * @throws GoodsIsNotExistException
     */
    @Override
    public void putawayGood(Goods goods) throws DAOException, GoodsIsNotExistException {
        TransactionManager tm = new TransactionManager();
        try {
            tm.openTransaction();
            if(goods==null){
                throw new GoodsIsNotExistException("商品不存在");
            }
            dao.addGoods(goods);
            tm.commit();
        } catch (DAOException e) {
            tm.rollback();
            e.printStackTrace();
            throw new DAOException(e);
        }
    }

    /**
     * 查询所有商品（spu）
     * @param s_id
     * @return
     * @throws DAOException
     * @throws GoodListIsNotExistException
     */
    @Override
    public List<Goods> selectAllGoods(int s_id) throws DAOException, GoodListIsNotExistException {
        List<Goods> list = dao.queryAllGood(s_id);
        if(list==null){
            throw new GoodListIsNotExistException("商家不存在商品");
        }
        return list;
    }
}
